package com.htc.adapter;

import java.io.File;
import java.util.ArrayList;

import com.htc.constants.Constants;
import com.htc.dlc.R;
import com.htc.dlc.SettingsActivity;
import com.htc.model.dto.WellnessOverviewDTO;
import com.htc.model.utils.FitnessModel;
import com.htc.webservice.PicassoUtils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WellnessOverviewAdapter extends ArrayAdapter<Object> {
	Activity Context;

	ArrayList<WellnessOverviewDTO> tempDayTime = null;
	ArrayList<Object> WnModelList;
	Context mContext;
	private ArrayList<Object> arrObject;

	public WellnessOverviewAdapter(Context context, int resource,
			ArrayList<Object> arrData) {
		super(context, resource, arrData);
		mContext = context;
		arrObject = arrData;
		WnModelList = arrData;
	}

	class viewHolder {
		ImageView iv_wellnessoverview_rowicon;
		TextView tv_wellnessoverview_title;
		TextView tv_wellnessoverview_description;
		TextView tv_wellnessover_timingheading;
		TextView tv_wellnessover_rowday;
	}

	public ArrayList<Object> getArrObject() {
		return arrObject;
	}

	public void setArrObject(ArrayList<Object> arrObject) {
		this.arrObject = arrObject;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return getArrObject().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return getArrObject().get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(
				R.layout.fragment_wellness_overvview_row, parent, false);
		viewHolder holder = new viewHolder();
		holder.iv_wellnessoverview_rowicon = (ImageView) rowView
				.findViewById(R.id.iv_wellnessoverview_rowicon);

		holder.tv_wellnessoverview_title = (TextView) rowView
				.findViewById(R.id.tv_wellnessoverview_title);

		holder.tv_wellnessoverview_description = (TextView) rowView
				.findViewById(R.id.tv_wellnessoverview_description);
		holder.tv_wellnessover_timingheading = (TextView) rowView
				.findViewById(R.id.tv_wellnessover_timingheading);

		holder.tv_wellnessover_rowday = (TextView) rowView
				.findViewById(R.id.tv_wellnessover_rowday);

		WellnessOverviewDTO dto = (WellnessOverviewDTO) arrObject.get(position);

		String path = (String) dto.getWellnessoverviewicon();
		String currentUrl = Constants.URL_IMAGE_PAHT + path;
		if (path != null) {
			String[] strs = path.split("/");
			File f = new File(Environment.getExternalStorageDirectory() + "/"
					+ Constants.APP_NAME + "/" + strs[0] + "/" + strs[1]);
			if (f.exists()) {
				if(f.length() == 0){
					f.delete();
					PicassoUtils.getInstance().showImageFromUrl(mContext,
							currentUrl, R.drawable.bgday,
							holder.iv_wellnessoverview_rowicon);
					// TODO: create folder?

					PicassoUtils.getInstance().saveImageFromUrl(
							mContext.getApplicationContext(), currentUrl, strs[1],
							strs[0]);
				}else{
					Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
					holder.iv_wellnessoverview_rowicon.setImageBitmap(bmp);
				}
				
			} else {
				PicassoUtils.getInstance().showImageFromUrl(mContext,
						currentUrl, R.drawable.bgday,
						holder.iv_wellnessoverview_rowicon);
				// TODO: create folder?

				PicassoUtils.getInstance().saveImageFromUrl(
						mContext.getApplicationContext(), currentUrl, strs[1],
						strs[0]);
			}
		}

		SettingsActivity st = new SettingsActivity();
		if (st.flag == 0) {
			holder.tv_wellnessoverview_title.setText(dto
					.getWellnessoverviewtitle());
			holder.tv_wellnessoverview_description.setText(dto
					.getWellnessoverviewDescription());

		} else if (st.flag == 1) {
			holder.tv_wellnessoverview_title.setText(dto
					.getWellnessoverviewtitleAr());
			holder.tv_wellnessoverview_description.setText(dto
					.getWellnessoverviewDescriptionAr());
		}
		FitnessModel md = FitnessModel.getInstance();
		ArrayList<WellnessOverviewDTO> arrDatatime;
		arrDatatime = md.getwellnessOverviewDayTime(dto.getWellnessclassId(),
				getContext());
		String str2 = "";
		String time = "";
		for (int j = 0; j < arrDatatime.size(); j++) {
			if (str2.length() == 2)
				str2 = str2 + ":00";
			String str1 = ((WellnessOverviewDTO) arrDatatime.get(j))
					.getWellnessoverviewtime();
			if ((str1.length() == 8) || (str1.length() == 7))
				str1 = str1.substring(0, 4);

			if (str1.length() == 2)
				str1 = str1 + ":00";

			if (str1.charAt(2) == '.')
				str1 = str1 + "0";
			time = (time
					+ ((WellnessOverviewDTO) arrDatatime.get(j))
							.getWellnessoverviewday() + " " + str1 + ", ");

			if (time.charAt(j) == '.')
				time = time.replace(".", ":");

		}
		String t = "";
		for (int i = time.length() - 1; i > 0; i--) {
			if (time.charAt(i) == ',') {
				t = time.replace(",", "");
				break;
			}

		}

		holder.tv_wellnessover_rowday.setText(t);
		return rowView;
	}

	public String removeLastChar(String s) {
		if (s == null || s.length() == 0) {
			return s;
		}
		return s.substring(0, s.length());
	}

	public void refreshData(ArrayList<Object> paramArrayList) {
		this.WnModelList = paramArrayList;
		notifyDataSetChanged();
	}
}